//
//  UITableView+OYIndexTip.m
//  ZhiXingLiPlus
//
//  Created by Apple on 2018/3/7.
//  Copyright © 2018年 wwtech. All rights reserved.
//

#import "UITableView+OYIndexTip.h"
#import "Aspects.h"
#import <objc/runtime.h>

@interface OYIndexTipManager : NSObject

@property (strong,nonatomic) UILabel * indexTipLabel;

@end

@implementation OYIndexTipManager

- (UILabel *)indexTipLabel{
    if(!_indexTipLabel){
        _indexTipLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
        _indexTipLabel.layer.masksToBounds = YES;
        _indexTipLabel.layer.cornerRadius = 4;
        _indexTipLabel.backgroundColor = [UIColor colorWithRed:1/225.0 green:205/255.0 blue:206/255.0 alpha:1];
        _indexTipLabel.textAlignment = NSTextAlignmentCenter;
        _indexTipLabel.textColor = [UIColor whiteColor];
        _indexTipLabel.font = [UIFont systemFontOfSize:24];
    }
    return _indexTipLabel;
}

@end


@interface UITableView ()

@property(strong, nonatomic) OYIndexTipManager *manager;

@end

@implementation UITableView (OYIndexTip)

#pragma mark - 黑魔法
- (void)setManager:(OYIndexTipManager *)tipManaget {
    objc_setAssociatedObject(self, @selector(manager), tipManaget, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (OYIndexTipManager *)manager {
    return objc_getAssociatedObject(self, @selector(manager));
}

- (void)addIndexTip {
    if (!self.manager) {
        self.manager = [[OYIndexTipManager alloc] init];
    }
    
    NSObject *delegate = self.delegate;
    if (!delegate) {
        NSException *excp = [NSException exceptionWithName:@"设置TableView代理delegate" reason:@"ZYXIndexTip >> 调用addIndexTip方法之前，UITableView 需要设置delegate" userInfo:nil];
        [excp raise]; // 抛出异常,提示错误
        
        return;
    }
    if (![delegate respondsToSelector:@selector(sectionIndexTitlesForTableView:)]) {
        NSException *excp = [NSException exceptionWithName:@"实现TableView代理方法" reason:@"ZYXIndexTip >> UITableView delegate 需要实现方法sectionIndexTitlesForTableView:" userInfo:nil];
        [excp raise]; // 抛出异常,提示错误
        return;
    }
    if (![delegate respondsToSelector:@selector(tableView:sectionForSectionIndexTitle:atIndex:)]) {
        NSException *excp = [NSException exceptionWithName:@"实现TableView代理方法" reason:@"ZYXIndexTip >> UITableView delegate 需要实现方法tableView:sectionForSectionIndexTitle:atIndex:" userInfo:nil];
        [excp raise]; // 抛出异常,提示错误
        return;
    }
    //hook
    [delegate aspect_hookSelector:@selector(tableView:sectionForSectionIndexTitle:atIndex:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> info, UITableView *tableView, NSString *title, NSInteger index) {
        [self handleWithIndexTitle:title atIndex:index];
        return index;
    } error:nil];
}

- (void)handleWithIndexTitle:(NSString *)title atIndex:(NSInteger)index{
    if ([title isEqualToString:UITableViewIndexSearch]) return;
    
    //找出TableView的索引视图UITableViewIndex
    UIView * view = (UIView *)self.subviews.lastObject;
    if(![NSStringFromClass([view class]) isEqualToString:@"UITableViewIndex"]){
        for(UIView * sview in self.subviews){
            if([NSStringFromClass([sview class]) isEqualToString:@"UITableViewIndex"]){
                view = sview;
                break;
            }
        }
    }
    
    CGPoint center = CGPointZero;
    OYLog(@"%@", NSStringFromCGRect(view.frame))
    center.x = -(view.frame.origin.x - view.frame.size.width) / 2.0;
    center.y = view.frame.size.height / 2.0;
    //添加索引提示视图到UITableViewIndex上
    self.manager.indexTipLabel.center = center;
    if (self.manager.indexTipLabel.superview != view){
        [view addSubview:self.manager.indexTipLabel];
    }
    //拦截TableView的索引视图UITableViewIndex的touches事件
    [view aspect_hookSelector:@selector(touchesBegan:withEvent:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> info, NSSet *touches, UIEvent *event) {
        [UIView animateWithDuration:0.15 animations:^{
            self.manager.indexTipLabel.alpha = 1;
        }];
    } error:nil];
    [view aspect_hookSelector:@selector(touchesMoved:withEvent:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> info, NSSet *touches, UIEvent *event) {
        self.manager.indexTipLabel.alpha = 1;
    } error:nil];
    [view aspect_hookSelector:@selector(touchesEnded:withEvent:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> info, NSSet *touches, UIEvent *event) {
        [UIView animateWithDuration:0.15 animations:^{
            self.manager.indexTipLabel.alpha = 0;
        }];
    } error:nil];
    [view aspect_hookSelector:@selector(touchesCancelled:withEvent:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> info, NSSet *touches, UIEvent *event) {
        [UIView animateWithDuration:0.15 animations:^{
            self.manager.indexTipLabel.alpha = 0;
        }];
    } error:nil];
    
    self.manager.indexTipLabel.text = title;
}

- (void)setTipLabelFont:(UIFont *)font {
    self.manager.indexTipLabel.font = font;
}

- (void)setTipLabelTextColor:(UIColor *)color {
    self.manager.indexTipLabel.textColor = color;
}

- (void)setTipLabelBackgroundColor:(UIColor *)color {
    self.manager.indexTipLabel.backgroundColor = color;
}

@end


















